//index.js
//获取应用实例
const app = getApp()
import util from "../../utils/util.js"

Page({
  data: {
    tabIndex: 0,
    searchHouse: {
      type: 1, // 房源类型
      order: 1, // 排序
      house_shape: null, // 户型
      page: 1, // 分页,
      noMore: true
    },
    searchShop: {
      type: 2, // 房源类型
      order: 1, // 排序
      page: 1, // 分页
      noMore: true
    },
    show: false,
    picker: null, //pick实例
    houseTypeList: [{ value: '', label: '全部'}],
    currentIndex: 0, //当前的户型选择picker index
    apartment: [],
    shop: [],
    loading: false,
  },
  onLoad: function () {
    this.getHouseShape()
    this.getListData(true, 0)
    this.getListData(true, 1)
    this.data.picker = this.selectComponent("#type-picker")
  },
 
  /**
   * 下拉刷新
   */
  onPullDownRefresh (e) {
    setTimeout(_ => {
      this.getListData(true)
    }, 1000)
  },
  /**
   * 上拉加载更多
   */
  onReachBottom(e) {
    console.log(12121)
    this.getListData(false)
  },
  /**
   * 获取户型类型, 在开头添加一个空的选项
   */
  getHouseShape() {
    let self = this
    app.globalData.request('get', 'applet/house/HouseShape').then(res => {
      self.setData({
        houseTypeList: [{ value: '', label: '全部' },...res.data]
      })
    })
  },
  /**
   * 切换 顶级tabs 数据不做任何变化 并且回滚至顶部
   */
  onChange(e) {
    this.data.tabIndex = e.detail.index // 保存顶级tabs的信息
    //this.setData({
      // 'searchForm.type': e.detail.index + 1,
      // 'searchForm.order': 1,
      // currentIndex: 0,
    //})
    // this.getListData(true)
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    });
  },
  /**
  * 查看详情
  */
  goToDetail (e) {
    wx.navigateTo({
      url: `./indexDetail/indexDetail?id=${e.currentTarget.dataset.id}&type=${e.currentTarget.dataset.type}&fromindex=${e.currentTarget.dataset.fromindex}`
    })
  },
  /**
   * 改变排序方式
   */
  changeSortType(e) {
    this.setData({
      [e.currentTarget.dataset.name+'.order']: e.currentTarget.dataset.type,
    })
    this.getListData(true)
  },
  /**
   * popup开启
   */
  showPopup() {
    this.setData({ 
      show: true,
     });
  },
  /**
   * 户型变更 修改currentIndex
   */
  onHouseTypeChange(e) {
    this.setData({
      currentIndex: e.detail.index
    });
  },
  /**
   * popup关闭 以当前currentIndex进行数据查询
   */
  onHouseTypeCancel() {
    this.setData({
      show: false,
    });
    this.selectComponent(`#type-picker`).setIndexes([this.data.currentIndex])
  },
  /**
   * popup 点击确认 进行筛选
   */
  onHouseTypeComfirm(e) {
    this.setData({
      show: false,
      currentIndex: e.detail.index
    });
    this.selectComponent(`#type-picker`).setIndexes([this.data.currentIndex])
    this.getListData(true)
  },
  /**
   * 根据状态类型获取列表数据
   */
  getListData(init, tabIndex) {
    let self = this
    // 格式化查询表单
    if ((tabIndex || self.data.tabIndex) == 0) {
      var form = self.data.searchHouse
      form.house_shape = self.data.houseTypeList[this.data.currentIndex].value
    } else {
      var form = self.data.searchShop
      form.house_shape = ''
    }

    if (init) { // 初始化查询
      if ((tabIndex || self.data.tabIndex) == 0) {
        this.setData({ 'searchHouse.page': 1, 'searchHouse.noMore': false })
      } else {
        this.setData({ 'searchShop.page': 1, 'searchShop.noMore': false })
      }
    } else {
      // 页码自增
      if ((tabIndex || self.data.tabIndex) == 0) {
        this.setData({
          'searchHouse.page': ++this.data.searchHouse.page
        })
      } else {
        this.setData({
          'searchShop.page': ++this.data.searchShop.page
        })
      }
    }

    if (!form.noMore) { // 仅在有更多时加载
      wx.showLoading({ title: '加载中' })
      this.setData({
        loading: true
      })
      console.log((tabIndex || self.data.tabIndex) == 0)
      app.globalData.request('get', 'applet/house/HousingList', form).then(res => {
        if ((tabIndex || self.data.tabIndex) == 0) {
          if (init) {
            this.setData({
              apartment: res.data.data,
              'searchHouse.noMore': res.data.no_more
            })
          } else {
            this.setData({
              apartment: [...self.data.apartment, ...res.data.data],
              'searchHouse.noMore': res.data.no_more
            })
          }
        } else {
          if (init) {
            this.setData({
              shop: res.data.data,
              'searchShop.noMore': res.data.no_more
            })
          } else {
            this.setData({
              shop: [...self.data.shop, ...res.data.data],
              'searchShop.noMore': res.data.no_more
            })
          }
        }
        this.setData({
          loading: false
        })
        // 加载完毕后强制关闭下拉刷新
        wx.hideLoading()
        wx.stopPullDownRefresh()
        
      }).catch(err => {
        this.setData({
          loading: false
        })
        wx.hideLoading()
      })
    }
  }
})
